package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.mapper.CartMapper;
import com.qf.myexception.ServiceException;
import com.qf.pojo.Cart;
import com.qf.pojo.Goods;
import com.qf.pojo.PatientPojo;
import com.qf.service.CartService;
import com.qf.service.GoodsService;
import com.qf.utils.Constant;
import com.qf.utils.ResultMsg;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {
    @Autowired
    CartMapper cartMapper;
    @Autowired
    GoodsService goodsService;

    @Override
    public ResultMsg findAll(HttpServletRequest request) {
        HttpSession session = request.getSession();
        PatientPojo patient = (PatientPojo) session.getAttribute(Constant.PATIENT_SESSION);
        if (patient == null)
            throw new ServiceException(Constant.LZY + 1, "请登录");
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("patient_id", patient.getPatientId());
        List<Cart> all = cartMapper.findAll(wrapper);
        return ResultMsg.SUCCESS(all);
    }

    @Override
    public ResultMsg findCartById(List<Integer> cartIds) {
        List<Cart> carts = this.listByIds(cartIds);
        return ResultMsg.SUCCESS(carts);
    }

    @Override
    public ResultMsg addGoods(Integer goodsId, HttpServletRequest request) {
        HttpSession session = request.getSession();
        PatientPojo patient = (PatientPojo) session.getAttribute(Constant.PATIENT_SESSION);
        if (patient == null)
            throw new ServiceException(Constant.LZY + 1, "请登录");

        QueryWrapper<Cart> wrapper = new QueryWrapper<Cart>();
        wrapper.eq("goods_id", goodsId);
        wrapper.eq("patient_id", patient.getPatientId());
        Cart cart = this.getOne(wrapper);
        if (cart != null)
            return this.modifyGoodsNum(goodsId, cart.getGoodsNum() + 1, request);
        ResultMsg one = goodsService.findOne(goodsId);
        Goods goods = (Goods) one.getData();
        Float goodsPrice = goods.getGoodsPrice();
        cart = new Cart(null, patient.getPatientId(), goodsId, 1, goodsPrice, null, goods.getGoodsName(),goods.getGoodsDesc(),null);
        boolean save = this.save(cart);
        if (save)
            return ResultMsg.SUCCESS(cart);
        return ResultMsg.FAILED(Constant.LZY + 1, "未知错误");
    }

    @Override
    public ResultMsg modifyGoodsNum(Integer goodsId, Integer num, HttpServletRequest request) {
        HttpSession session = request.getSession();
        PatientPojo patient = (PatientPojo) session.getAttribute(Constant.PATIENT_SESSION);
        if (patient == null)
            throw new ServiceException(Constant.LZY + 1, "请登录");
        ResultMsg one = goodsService.findOne(goodsId);
        Goods goods = (Goods) one.getData();
        Float goodsPrice = goods.getGoodsPrice();

        BigDecimal goodsPriceBig = new BigDecimal(goodsPrice + "");
        BigDecimal goodsNumBig = new BigDecimal(num + "");
        BigDecimal goodsValueBig = goodsNumBig.multiply(goodsPriceBig);
        float goodsValue = goodsValueBig.floatValue();

        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("patient_id", patient.getPatientId());
        wrapper.eq("goods_id", goodsId);
        Cart cart = this.getOne(wrapper);
        cart.setGoodsNum(num);
        cart.setGoodsValue(goodsValue);
        boolean b = this.updateById(cart);
        if (b)
            return ResultMsg.SUCCESS(cart);
        return ResultMsg.FAILED(Constant.LZY + 1, "修改失败,未知错误");
    }

    @Override
    public ResultMsg removeGoods(Integer cartId) {
        boolean cart_id = this.remove(new QueryWrapper<Cart>().eq("cart_id", cartId));
        if (cart_id)
            return ResultMsg.SUCCESS();
        return ResultMsg.FAILED(Constant.LZY + 1, "未知错误");
    }


}
